package com.yidu.t299.dao.xp;
import com.yidu.t299.domain.xp.Class;
import com.yidu.t299.domain.xp.Record;
import com.yidu.t299.domain.xp.School;
import com.yidu.t299.domain.xp.StudentChange;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import java.util.List;

/*
使用Mybatis逆向生成自动生成 后续自己更改
 */
@Mapper
public interface StudentChangeMapper {
    /**
     * 根据转班编号删除转班信息
     * @param studentchangeId
     * @return
     */
    @Delete({
        "delete from studentchange",
        "where studentchange_id = #{studentchangeId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer studentchangeId);

    /**
     * 增加转班信息
     * @param record
     * @return
     */
    @Insert({
        "insert into studentchange (studentchange_id, student_id, ",
        "studentchange_date, class_id, ",
        "school_id, studentchange_backfield, ",
        "studentchange_backclass, record_id)",
        "values (#{studentchangeId,jdbcType=INTEGER}, #{studentId,jdbcType=INTEGER}, ",
        "#{studentchangeDate,jdbcType=VARCHAR}, #{classId,jdbcType=INTEGER}, ",
        "#{schoolId,jdbcType=INTEGER}, #{studentchangeBackfield,jdbcType=VARCHAR}, ",
        "#{studentchangeBackclass,jdbcType=VARCHAR}, #{recordId,jdbcType=INTEGER})"
    })
    int insert(StudentChange record);

    /**
     * 根据转班编号查询指定信息
     * @param studentchangeId 转班编号
     * @return
     */
    @Select({
        "select",
        "studentchange_id, student_id, studentchange_date, class_id, school_id, studentchange_backfield, ",
        "studentchange_backclass, record_id",
        "from studentchange",
        "where studentchange_id = #{studentchangeId,jdbcType=INTEGER}"
    })
    @Results(id = "StudentChangeMap",value = {
        @Result(column="studentchange_id", property="studentchangeId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="student_id", property="studentId", jdbcType=JdbcType.INTEGER),
        @Result(column="studentchange_date", property="studentchangeDate", jdbcType=JdbcType.VARCHAR),
        @Result(column="class_id", property="classId", jdbcType=JdbcType.INTEGER),
        @Result(column="school_id", property="schoolId", jdbcType=JdbcType.INTEGER),
        @Result(column="studentchange_backfield", property="studentchangeBackfield", jdbcType=JdbcType.VARCHAR),
        @Result(column="studentchange_backclass", property="studentchangeBackclass", jdbcType=JdbcType.VARCHAR),
        @Result(column="record_id", property="recordId", jdbcType=JdbcType.INTEGER)
    })
    StudentChange selectByPrimaryKey(Integer studentchangeId);

    /**
     * 按条件分页查询转班数据集合
     * @param rows 每页的行数
     * @param page  页码
     * @param condition 条件参数
     * @return 转班对象集合
     */
    @Select("select * from studentchange cm " +
            "join student_manage c " +
            "on cm.student_id=c.student_id " +
            "join class sm " +
            "on sm.class_id=cm.class_id " +
            "join school sc " +
            "on sc.school_id=cm.school_id " +
            "join  record re " +
            "on re.record_id=cm.record_id where 1=1 ${condition} limit #{page},#{rows}")
    @Results({
            @Result(column = "studentchange_id",property = "studentchangeId",jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "student_id",property ="studentId" ,jdbcType = JdbcType.INTEGER),
            @Result(column = "studentchange_date",property ="studentchangeDate" ,jdbcType = JdbcType.VARCHAR),
            @Result(column = "class_id",property ="classId" ,jdbcType = JdbcType.INTEGER),
            @Result(column = "school_id",property ="schoolId" ,jdbcType = JdbcType.INTEGER),
            @Result(column = "studentchange_backfield",property ="studentchangeBackfield" ,jdbcType = JdbcType.VARCHAR),
            @Result(column = "studentchange_backclass",property ="studentchangeBackclass" ,jdbcType = JdbcType.VARCHAR),
            @Result(column = "record_id",property ="recordId" ,jdbcType = JdbcType.INTEGER),
            @Result(column = "student_name",property ="studentName" ,jdbcType = JdbcType.VARCHAR),
            @Result(column = "class_name",property ="className" ,jdbcType = JdbcType.VARCHAR),
            @Result(column = "school_name",property ="schoolName" ,jdbcType = JdbcType.VARCHAR),
            @Result(column = "record_name",property ="recordName" ,jdbcType = JdbcType.VARCHAR)
    })
    List<StudentChange> selectByPage(@Param("rows") int rows, @Param("page") int page, @Param("condition") String condition);

    /**
     * 按条件查询studentchange表中数据行数
     * 因为涉及了多表所以这里也需要多表联查不然无法设置条件
     * @param condition 条件参数
     * @return 数据行数
     */
    @Select("select count(*) from studentchange cm " +
            "join student_manage c " +
            "on cm.student_id=c.student_id " +
            "join class sm " +
            "on sm.class_id=cm.class_id " +
            "join school sc " +
            "on sc.school_id=cm.school_id " +
            "join  record re " +
            "on re.record_id=cm.record_id where 1=1 ${condition}")
    int selectCount(@Param("condition") String condition);

    /**
     * 根据转班信息修改其数据
     * @param record 退班对象
     * @return 影响的行数(返回1则成功)
     */
    @Update({
        "update studentchange",
        "set student_id = #{studentId,jdbcType=INTEGER},",
          "studentchange_date = #{studentchangeDate,jdbcType=VARCHAR},",
          "class_id = #{classId,jdbcType=INTEGER},",
          "school_id = #{schoolId,jdbcType=INTEGER},",
          "studentchange_backfield = #{studentchangeBackfield,jdbcType=VARCHAR},",
          "studentchange_backclass = #{studentchangeBackclass,jdbcType=VARCHAR},",
          "record_id = #{recordId,jdbcType=INTEGER}",
        "where studentchange_id = #{studentchangeId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(StudentChange record);

    //以下是获取下拉框数据的方法

    /**
     * 得到所有校区名称信息
     * @return 校区名称集合
     */
    @Select("select school_name from school")
    @Results({
            @Result(id = true,column = "school_name",property = "schoolName")
    })
    List<School> selectAllGetSchoolName();

    /**
     * 得到所有教师名称信息
     * @return 教师名称集合
     */
    @Select("select record_name from record")
    @Results({
            @Result(id = true,column = "record_name",property = "recordName")
    })
    List<Record> selectAllGetRecordName();

    /**
     * 得到所有班级名称信息
     * @return 校区名称集合
     */
    @Select("select class_name from class")
    @Results({
            @Result(id = true,column = "class_name",property = "className")
    })
    List<Class> selectAllGetClassName();

    /**
     *针对学员表(student_manage)进行操作
     * 根据学员名称查找其编号
     * @param studentName 学员名称
     * @return 影响的行数(>1:成功)
     */
    @Select("select student_id from student_manage where student_name=#{studentName}")
    int selectByStudentName(@Param("studentName") String studentName);

    /**
     *针对校区表(school)进行操作
     * 根据校区名称查找其编号
     * @param schoolName 校区名称
     * @return 影响的行数(>1:成功)
     */
    @Select("select school_id from school where school_name=#{schoolName}")
    int selectBySchoolName(@Param("schoolName") String schoolName);

    /**
     *针对班级表(class)进行操作
     * 根据班级名称查找其编号
     * @param className 班级名称
     * @return 影响的行数(>1:成功)
     */
    @Select("select class_id from class where class_name=#{className}")
    int selectByClassName(@Param("className") String className);

    /**
     *针对教师表(record)进行操作
     * 根据教师名称查找其编号
     * @param recordName 教师名称
     * @return 影响的行数(>1:成功)
     */
    @Select("select record_id from record where record_name=#{recordName}")
    int selectByRecordName(@Param("recordName") String recordName);
}